<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/WEB-INF/view/include/inc.jsp"%>
<!DOCTYPE html >
<html>
<head>
<%@ include file="/WEB-INF/view/jQgrid.jsp"%>
    <link rel="stylesheet" href="<%=contextPath %>/static/autosuggest/css/autosuggest.css">
    <script src="<%=contextPath %>/static/autosuggest/js/autosuggest.js"></script>
</head>
<body style="background-color: white;" class="gray-bg">
	<div class="ibox-content">
		<div class="row-fluid">
			<div class="col-sm-12">
				<input type="hidden" id="tempids" value="${ids }" />
		        <input type="hidden" id="tempnames" value="" /> 
				<form class="form-horizontal m-t " id="signupForm" method="post"
					enctype="multipart/form-data">
					<input type="hidden" id="talents_id" name="talentsId" value="${talentsId}" />
					<fieldset>
						<legend>工作经历</legend>
						<div class="workinput">
						<c:forEach items="${workExperiences }" var="work" varStatus="num">
						<div class="addexp${num.count }">
						<input id="id${num.count }"  name="id${num.count}" type="hidden" value="${work.id }"/>
						<div class="form-group">
								<label class="col-sm-2 control-label">起止时间：</label>
								<div class="col-sm-10 form-inline">
									<input type="text" class="form-control datetime"
										id="w_startDate${num.count}" name="startDate${num.count}" readonly="readonly"
										placeholder="请选择日期"  value="${work.startDate}"/> - 
										<select class="form-control" onchange="chooseDate(${num.count});" id="choosedate${num.count }"><option value="0" <c:if test="${work.endDate =='至今' }">selected="selected"</c:if>>至今</option><option value="1" <c:if test="${work.endDate !='至今' }">selected="selected"</c:if>>选择年月</option></select>
										<input type="text"
										class="form-control datetime" id="w_endDate${num.count}"
										name="endDate${num.count}" readonly="readonly" placeholder="请选择日期" value="${work. endDate}" <c:if test="${work.endDate =='至今' }">style="display:none;"</c:if>/>
										
										
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">公司名称：</label>
								<div class="col-sm-8">
									<input type="text"  class="form-control company" id="w_companyName${num.count}" name="companyName${num.count}"  value="${work.companyName }"/>
									<input type="hidden"  class="form-control" id="w_companyId${num.count}" name="companyId${num.count}" value="${work.company.id }" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">公司描述：</label>
								<div class="col-sm-10">
								   <textarea rows="3" cols="3" class="form-control" id="w_description${num.count}"
										name="description${num.count}">${work.description }</textarea>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">工作职能：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_job${num.count}"
										name="job${num.count}" value="${work.job}"/>
								</div>
								<label class="col-sm-2 control-label">职位名称：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_position${num.count}"
										name="position${num.count}" value="${work.position}"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">部门名称：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_depId${num.count}"
										name="depId${num.count}" value="${work.depId}"/>
								</div>
								<label class="col-sm-2 control-label">工作地点：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_workplace${num.count}"
										name="workplace${num.count}" value="${work.workplace}"/>
								</div>
							</div>
							<div class="form-group">
							   <label class="col-sm-2 control-label">汇报上级：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_report${num.count}"
										name="report${num.count}" value="${work.report}"/>
								</div>
								<label class="col-sm-2 control-label">下属人数：</label>
								<div class="col-sm-4">
									<input type="number" class="form-control" id="w_subordinateNum${num.count}"
										name="subordinateNum${num.count}" value="${work.subordinateNum}" onkeyup="onlyNumber(this)" onblur="onlyNumber(this)"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">工作职责：</label>
								<div class="col-sm-10">
									<textarea rows="3" cols="3" class="form-control"
										id="w_responsibility${num.count}" name="responsibility${num.count}">${work.responsibility}</textarea>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">工作业绩：</label>
								<div class="col-sm-10">
									<textarea rows="3" cols="3" class="form-control"
										id="t_performance${num.count}" name="performance${num.count}">${work.performance}</textarea>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">项目经验：</label>
								<div class="col-sm-10">
								    <input type="text" id="w_projectName${num.count}" name="projectNames${num.count}" value="${work.projectNames }" class="form-control" placeholder="添加项目经验" onclick="addExpProject(${num.count});" readonly="readonly" >
								    <input type="hidden" id="w_projectId${num.count}" name="project${num.count}" value="${work.project }"/>
								</div>
							</div>
							<div class="form-group" style="text-align: center;">
								<div class="col-sm-12">
								      <button type="button" class="btn btn-primary btn-sm closebtn" onclick="removehtml('${num.count}');"><i class="icon-minus"></i>删除</button>
								</div>
							</div>
							<div class="form-group">
								<div class="col-sm-12" style="border: 1px solid #e5e6e7;">
								</div>
							</div>
							</div>
						</c:forEach>
						<c:if test="${worknum ==0 }">
						<div class="form-group">
								<label class="col-sm-2 control-label">起止时间：</label>
								<div class="col-sm-10 form-inline">
									<input type="text" class="form-control datetime"
										id="w_startDate1" name="startDate1" readonly="readonly"
										placeholder="请选择日期" /> - <select class="form-control" onchange="chooseDate(1);" id="choosedate1"><option value="0" >至今</option><option value="1" >选择年月</option></select><input type="text"
										class="form-control datetime" id="w_endDate1"
										name="endDate1" readonly="readonly" placeholder="请选择日期"  style="display: none;"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">公司名称：</label>
								<div class="col-sm-10">
									<input type="text"  class="form-control company" id="w_companyName1" name="companyName1" />
									<input type="hidden"  class="form-control" id="w_companyId1" name="companyId1" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">公司描述：</label>
								<div class="col-sm-10">
								   <textarea rows="3" cols="3" class="form-control" id="w_description1"
										name="description1"></textarea>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">工作职能：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_job1"
										name="job1" />
								</div>
								<label class="col-sm-2 control-label">职位名称：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_position1"
										name="position1" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">部门名称：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_depId1"
										name="depId1" />
								</div>
								<label class="col-sm-2 control-label">工作地点：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_workplace1"
										name="workplace1" />
								</div>
							</div>
							<div class="form-group">
							   <label class="col-sm-2 control-label">汇报上级：</label>
								<div class="col-sm-4">
									<input type="text" class="form-control" id="w_report1"
										name="report1" />
								</div>
								<label class="col-sm-2 control-label">下属人数：</label>
								<div class="col-sm-4">
									<input type="number" class="form-control" id="w_subordinateNum1"
										name="subordinateNum1" value="0" onkeyup="onlyNumber(this)" onblur="onlyNumber(this)"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">工作职责：</label>
								<div class="col-sm-10">
								      <textarea rows="3" cols="3" class="form-control"
										id="w_responsibility1" name="responsibility1"></textarea>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">工作业绩：</label>
								<div class="col-sm-10">
									<textarea rows="3" cols="3" class="form-control"
										id="t_performance1" name="performance1"></textarea>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">项目经验：</label>
								<div class="col-sm-10">
									<input type="text" id="w_projectName1" class="form-control" name="projectNames1" placeholder="添加项目经验" onclick="addExpProject(1);" readonly="readonly"/>
								    <input type="hidden" id="w_projectId1" name="project1"/>
								</div>
							</div>
							<div class="form-group">
								<div class="col-sm-12" style="border: 1px solid #e5e6e7;">
								</div>
							</div>
						</c:if>	
						</div>
					</fieldset>
					<div class="form-group" style="text-align: center;">
						<div class="col-sm-12">
							<button type="button" class="btn btn-primary btn-sm addbtn"
								id="addinput">
								<i class="icon-plus"></i>增加工作经历
							</button>
						</div>
					</div>
					<div class="form-group" style="text-align: center;">
						<div class="col-sm-12">
							<button class="btn btn-primary" type="button" id="sub">提交</button>
							<button class="btn btn-primary" type="button"
								onclick="top.Dialog.close();">取消</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
	<script type="text/javascript">
	$(top.hangge());
	 $(function () {
		 $("#w_companyName1").autosuggest({
	            url: '<%=contextPath%>/company/getJsonCompany',
	                minLength: 1,
	                maxNum: 4,
	                align: 'center',
	                method: 'post',
	                highlight: true,
	                immediate: true,
	                queryParamName: 'name',
	                nextStep: function (ele) {
	                    var tid = ele.data('id'); 
	                    $("#w_companyId1").val(tid);
	                },
	                split: ' ',
	                open: function(){
	                    //console.log("start open");
	                },
	                close: function(){
	                    //console.log("start close");
	                }
	            });
		 $('.datetime').datetimepicker({
			 format: 'yyyy-mm',
		        autoclose: true,
		        todayBtn: true,
		        startView: 'year',
		        minView:'year',
		        maxView:'decade',
		        language:  'zh-CN',
		    	autoclose: 1//选择后自动关闭
		  });
		 var y=1;
		 var worknum = ${worknum};
		 if(worknum == 0){y=2};
		 if(worknum > 0){y= worknum+y};
		 $("#addinput").click(function(){
			 var html='<div class="addexp'+y+'"><div class="form-group"><label class="col-sm-2 control-label">起止时间：</label>';
				 html+='<div class="col-sm-10 form-inline"><input type="text" class="form-control datetime"id="w_startDate'+y+'" name="startDate'+y+'" readonly="readonly"placeholder="请选择日期" /> - ';
				 html+='<select class="form-control" onclick="chooseDate('+y+');" id="choosedate'+y+'"><option value="0" >至今</option><option value="1" selected="selected">选择年月</option></select><input type="text"class="form-control datetime" id="w_endDate'+y+'"name="endDate'+y+'" readonly="readonly" placeholder="请选择日期"/></div></div>';
				 html+='<div class="form-group"><label class="col-sm-2 control-label">公司名称：</label>';
				 html+='<div class="col-sm-6"><input type="text"  class="form-control company" id="w_companyName'+y+'" name="companyName'+y+'"/></div></div>';
				 html+='<div class="form-group"><label class="col-sm-2 control-label">公司描述：</label>';
		         html+='<div class="col-sm-10"><textarea rows="3" cols="3" class="form-control" id="w_description'+y+'"name="description'+y+'" /></textarea></div></div>';
		         html+='<div class="form-group"><label class="col-sm-2 control-label">工作职能：</label>';
		         html+='<div class="col-sm-4"><input type="text" class="form-control" id="w_job'+y+'"name="job'+y+'" /></div>';
		         html+='<label class="col-sm-2 control-label">职位名称：</label>';
		         html+='<div class="col-sm-4"><input type="text" class="form-control" id="w_position'+y+'"name="position'+y+'" /></div></div>';
		         html+='<div class="form-group"><label class="col-sm-2 control-label">部门名称：</label>';
		         html+='<div class="col-sm-4"><input type="text" class="form-control" id="w_depId'+y+'"name="depId'+y+'" /></div>';
		         html+='<label class="col-sm-2 control-label">工作地点：</label>';
		         html+='<div class="col-sm-4"><input type="text" class="form-control" id="w_workplace'+y+'"name="workplace'+y+'" /></div></div>';
		         html+='<div class="form-group"><label class="col-sm-2 control-label">汇报上级：</label>';
		         html+='<div class="col-sm-4"><input type="text" class="form-control" id="w_report'+y+'"name="report'+y+'" /></div>';
		         html+='<label class="col-sm-2 control-label">下属人数：</label>';
		         html+='<div class="col-sm-4"><input type="number" class="form-control" id="w_subordinateNum'+y+'"name="subordinateNum'+y+'" value="0" onkeyup="onlyNumber(this)" onblur="onlyNumber(this)"/></div></div>';
		         html+='<div class="form-group"><label class="col-sm-2 control-label">工作职责：</label>';
		         html+='<div class="col-sm-10"><textarea rows="3" cols="3" class="form-control" id="w_responsibility'+y+'"name="responsibility'+y+'" /></textarea></div></div>';
		         html+='<div class="form-group"><label class="col-sm-2 control-label">工作业绩：</label>';
		         html+='<div class="col-sm-10"><textarea rows="3" cols="3" class="form-control"id="t_performance'+y+'" name="performance'+y+'"></textarea></div></div>';
		         html+='<div class="form-group"><label class="col-sm-2 control-label">项目经验：</label>';
		         html+='<div class="col-sm-10"><input type="text" id="w_projectName'+y+'" class="form-control" name="projectNames'+y+'" placeholder="添加项目经验" onclick="addExpProject('+y+');" readonly="readonly"/>';
		         html+='<input type="hidden" id="w_projectId'+y+'" name="project'+y+'"/></div></div>';
		         html +='<div class="form-group" style="text-align: center;"><div class="col-sm-12"><button type="button" class="btn btn-primary btn-sm closebtn" onclick="removehtml('+y+');"><i class="icon-minus"></i>删除</button></div></div>'
		         html+='<div class="form-group"><div class="col-sm-12" style="border: 1px solid #e5e6e7;"></div></div></div>';
		         y =y+1;
			 $(html).appendTo(".workinput");
			 choosewCompany(y);
			 var num = y-1;
			 var old = y-2;
			 $('.datetime').datetimepicker({
				    format: 'yyyy-mm',
			        autoclose: true,
			        todayBtn: true,
			        startView: 'year',
			        minView:'year',
			        maxView:'decade',
			        language:  'zh-CN',
			  });
			 var time = $("#w_startDate"+old).val();
			 //var newyear = time.substring(0,4)-4;//获取年
			 $("#w_endDate"+num).datetimepicker("update",time);//赋值
			 //$("#w_startDate"+num).datetimepicker("update",newyear+'-01');//赋值
			  $('#w_startDate'+num).datetimepicker({
			    	 format: 'yyyy-mm',
				        autoclose: true,
				        todayBtn: true,
				        startView: 'year',
				        minView:'year',
				        maxView:'decade',
				        language:  'zh-CN',
			  }).on('hide', function(event) {
				  //var year = event.date.getFullYear();//获取年
				  //var newyear = year+4;
				  //$("#w_endDate"+num).datetimepicker("update",newyear+'-01-01');//赋值
				}); 
			    $("#w_companyName"+num).autosuggest({
		            url: '<%=contextPath%>/company/getJsonCompany',
		                minLength: 1,
		                maxNum: 4,
		                align: 'center',
		                method: 'post',
		                highlight: true,
		                immediate: true,
		                queryParamName: 'name',
		                nextStep: function (ele) {
		                    var tid = ele.data('id'); 
		                    $("#w_companyId"+num).val(tid);
		                },
		                split: ' ',
		                open: function(){
		                    //console.log("start open");
		                },
		                close: function(){
		                    //console.log("start close");
		                }
		            });
		 });
	        $("#sub").click(function () {
	            $("#signupForm").ajaxSubmit({
	                url: '<%=contextPath%>/work/update',
	                data:{num:y},
					type : 'post',
					dataType : "json",
				    beforeSend: function () {
				        // 禁用按钮防止重复提交
						$("#sub").attr("disabled","disabled");
						swal("信息已经提交，请等待后台处理~！");
				    },
					success : function(data) {
						if (data.code == 200) {
							swal({
								title : data.message,
								type : "success",
								showCancelButton : false,
								confirmButtonText : "确定",
								animation : "slide-from-bottom",
								text: "2秒后自动关闭。", 
		      	    			timer: 2000, 
								closeOnConfirm : true
							}, function() {
								top.Dialog.close();
							});
						} else {
							swal("错误", data.message, "error");
						}  
					},    
					complete: function () {
				        $("#sub").removeAttr("disabled");
				    },
				    error: function (data) {
				    	swal("错误", data.message, "error");
				    }
				});
			});
		});
	 function removehtml(num) {
		 var tid = $("#id"+num).val();
		 if(tid!=null && tid!=''){
			 swal({
				    title: "您确定要删除该条记录吗",
				    text: "删除后将无法恢复，请谨慎操作！",
				    type: "warning",
				    showCancelButton: true,
				    confirmButtonColor: "#DD6B55",
				    confirmButtonText: "删除",
				    animation:"slide-from-bottom",
				    cancelButtonText:"取消",
				    closeOnConfirm: true
				},function (){
					 $.ajax({
						 url:'<%=contextPath%>/work/delete?id='+tid,
						 dataType:'json',
						 success:function(data){
							if(data.code==200){
								swal("删除成功");
								$(".addexp"+num).remove();//事先存好了对象就方便删除了
					    	}else{
					    		swal("出现错误，请稍后再试！");
					    	}
						 }
					 });
				});
		 }else{
			 $(".addexp"+num).remove();//事先存好了对象就方便删除了
		 }
	}
	 function choosewCompany(num,id) {
		 $.ajax({
	      		url:'<%=contextPath%>/company/getJson',
	      	    type:'post',
	      	    dataType:'json',
	      	    success:function(data){
	      	    	var html='<option  value="">==请选择==</option>';
	      	    	for (var i = 0; i < data.length; i++) {
	      	    		var tempid =data[i].id;
	      	    		if(id == tempid){
	      	    			html+='<option  value="'+data[i].id+'" selected="selected">'+data[i].name+'</option>';
	      	    		}else{
	      	    			html+='<option  value="'+data[i].id+'">'+data[i].name+'</option>';
	      	    		}
						}
	      	    	$("#w_companyId"+num).html(html);
	      	    	$("#w_companyId"+num).selectpicker('refresh');
	      	    }
	      	});
	}
	 function chooseDate(num) {
		 var tval = $("#choosedate"+num).val();
		if(tval == 1){
			$("#w_endDate"+num).attr("value","");
			$("#w_endDate"+num).show();
		}
		if(tval == 0){
			$("#w_endDate"+num).attr("value","");
			$("#w_endDate"+num).hide();
		}
	}
	 function addCompany(num) {
		 var name = $("#w_companyName"+num).val();
		 if(name !=""){
			 $.ajax({
					url:'<%=contextPath%>/company/add',
					data:{name:name},
					type:'post',
					dataType:'json',
		      	    success:function(data){
		      	    	if(data.code ==200){
		      	    		swal({
		      	    			title:data.message,
		      	    			text: "1秒后自动关闭。", 
		      	    			timer: 1000, 
		      				    showConfirmButton: false
		      	    		});
		      	    		$("#w_companyId"+num).val(data.id);
		      	    	}else {
		      	    		swal({
		      	    			title:data.message,
		      	    			text: "1秒后自动关闭。", 
		      	    			timer: 1000, 
		      				    showConfirmButton: false
		      	    		});
						}
		      	    }
				});
		 }else {
			 swal({
	    			title:"名称未填写",
	    		});
		}
	}
	 function onlyNumber(obj){
		//只能输入正整数.
		obj.value = obj.value.replace(/[^0-9]/g,'');
	 }
	 function addExpProject(num){
		 var projectId = $("#w_projectId"+num).val();
		 var workId = $("#id"+num).val();
		 if(typeof(workId)=="undefined"){ 
			 workId="none"+num;
		 }
	   	 var diag = new top.Dialog();
		 diag.Drag=true;
		 diag.Title ="添加项目经验";
		 diag.URL = '<%=contextPath %>/expProject/chooseProject?workExpId='+workId+'&id='+projectId;
		 diag.Width = 700;
		 diag.Height = 650;
		 diag.CancelEvent = function(){ //关闭事件
			 var tempid = diag.innerFrame.contentWindow.document.getElementById('tempids').value;
			 var tempname = diag.innerFrame.contentWindow.document.getElementById('tempnames').value;
			if(tempid.length!==0){
					$("#w_projectId"+num).val(tempid);
					$("#w_projectName"+num).val(tempname);
			} 
			diag.close();
		 };
		 diag.show();
	 }
	</script>
</body>
</html>